﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.CallBackManage
{
    public partial class CallBackHandle : Common.PageBase
    {
        T_CALLBACK_TASK_BLL callBackTaskBll = new T_CALLBACK_TASK_BLL();
        T_CONTACT_HISTORY_BLL bll = new T_CONTACT_HISTORY_BLL();
        T_DICTIONARY_BLL dBll = new T_DICTIONARY_BLL();

        private UserLoginInfo _userLoginInfo;

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            //PageHelper.SetGoBackUrl(this.Page, btnCancel, );
            //PageHelper.SetPopCalender(this.Page, txtCREATETIME);

            UserLoginInfo.CheckLoginInfo();
            _userLoginInfo = UserLoginInfo.GetLoginInfo();

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnSave.Visible = UserLoginInfo.IsHasPermissionRight(StaticConstant.CallBackHandlePageClass, "CallBackTaskSave");
            btnOver.Visible = UserLoginInfo.IsHasPermissionRight(StaticConstant.CallBackHandlePageClass, "CallBackTaskOver");
            if (!IsPostBack)
            {
                ddlISHASPURCHASEBinding();
                ddlSOURCEBinding();
                ddlCAUSEANALYSISBinding();
                ddlCASE_STATUSBinding();

                pageDataInit();
            }

        }

        #region 页面初始化数据绑定


        /// <summary>
        /// 是否已购机
        /// DDLs the ISHASPURCHASE binding.
        /// </summary>
        private void ddlISHASPURCHASEBinding()
        {
            ddlISHASPURCHASE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("IsHasPurchase"));
            ddlISHASPURCHASE.DataTextField = "DictValue";
            ddlISHASPURCHASE.DataValueField = "DictKey";
            ddlISHASPURCHASE.DataBind();
            ddlISHASPURCHASE.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 客户购车信息来源
        /// DDLs the SOURCE binding.
        /// </summary>
        private void ddlSOURCEBinding()
        {
            ddlSOURCE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("SaleVehicleInfoSource"));
            ddlSOURCE.DataTextField = "DictValue";
            ddlSOURCE.DataValueField = "DictKey";
            ddlSOURCE.DataBind();
            ddlSOURCE.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// 客户原因分析
        /// DDLs the CAUSEANALYSIS binding.
        /// </summary>
        private void ddlCAUSEANALYSISBinding()
        {
            ddlCAUSEANALYSIS.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CauseAnalysis"));
            ddlCAUSEANALYSIS.DataTextField = "DictValue";
            ddlCAUSEANALYSIS.DataValueField = "DictKey";
            ddlCAUSEANALYSIS.DataBind();
            ddlCAUSEANALYSIS.Items.Insert(0, new ListItem("", ""));
        }



        /// <summary>
        /// DDLs the CAS e_ STATUS binding.
        /// </summary>
        private void ddlCASE_STATUSBinding()
        {
            ddlCASE_STATUS.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CallBackTaskStatus"));
            ddlCASE_STATUS.DataTextField = "DictValue";
            ddlCASE_STATUS.DataValueField = "DictKey";
            ddlCASE_STATUS.DataBind();
            ddlCASE_STATUS.Items.Insert(0, new ListItem("", ""));        

        }




        #endregion

        private void pageDataInit()
        {
            string customerId = PageHelper.GetQueryString(this.Page, "CUSTOMERID");
            string taskId = PageHelper.GetQueryString(this.Page, "TASKID");
            lblCustomerID.Text = customerId;
            lblTaskId.Text = taskId;

            T_CALLBACK_TASK_Entity entity = callBackTaskBll.GetList(String.Format(" and CustomerId = '{0}' and Task_Id = '{1}' ", customerId, taskId)).FirstOrDefault();
            if (entity != null)
            {
                txtCUSTOMERNAME.Text = entity.CUSTOMERNAME;
                txtCALLID.Text = "";
                ddlCASE_STATUS.SelectedValue = "";
                txtREMARKS.Text = "";
                switch ((CallBackTaskStatus)Enum.Parse(typeof(CallBackTaskStatus), entity.CASE_PROCESS.ToString()))
                {
                    case CallBackTaskStatus.已完成:
                        {
                            btnOver.Visible = false;
                            btnSave.Visible = false;
                        }
                        break;
                }

            }


        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string customerId = PageHelper.GetQueryString(this.Page, "CUSTOMERID");
            string taskId = PageHelper.GetQueryString(this.Page, "TASKID");
             T_CALLBACK_TASK_Entity taskEntity = callBackTaskBll.GetList(String.Format(" and CustomerId = '{0}' and Task_Id = '{1}' ", customerId, taskId)).FirstOrDefault();
             if (taskEntity != null)
             {
                 PageHelper.GetControlValue<T_CALLBACK_TASK_Entity>(this.Page, ref taskEntity);
                 taskEntity.FIRST_CONTACT_DATE = taskEntity.FIRST_CONTACT_DATE.HasValue? taskEntity.FIRST_CONTACT_DATE.Value: DateTime.Now;
                 taskEntity.LAST_CONTACT_DATE = DateTime.Now;
                 taskEntity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                 taskEntity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                 taskEntity.LASTMODIFYTIME = DateTime.Now;
                 taskEntity.CONTACTNUM = taskEntity.CONTACTNUM.HasValue ? taskEntity.CONTACTNUM.Value + 1 : 1;
                 taskEntity.CASE_STATUS = ddlCASE_STATUS.SelectedValue.Trim();
                 taskEntity.REMARKS = txtREMARKS.Text.Trim();
                 Button btn = sender as Button;
                 switch (btn.ID)
                 {
                     case "btnOver":
                         {
                             taskEntity.CASE_PROCESS = (int)CallBackTaskStatus.已完成;                             
                         }
                         break;
                     case "btnSave":
                         {
                             taskEntity.CASE_PROCESS = (int)CallBackTaskStatus.未完成;
                         }
                         break;
                 }

                 T_CONTACT_HISTORY_Entity entity = new T_CONTACT_HISTORY_Entity();
                 bll._dal.ObjectCopyTo(taskEntity, entity);
                 entity.CALLID = txtCALLID.Text;
                 entity.CONTACTNUMBER = txtCONTACTNUMBER.Text.Trim();
                 entity.CASE_STATUS = ddlCASE_STATUS.SelectedValue.Trim();
                 entity.STATUS = ddlCASE_STATUS.SelectedItem.Text;
                 entity.REMARKS = txtREMARKS.Text;
                 entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                 entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                 entity.CREATETIME = DateTime.Now;


                 long rel = bll.Insert(entity);
                 if (rel > 0)
                 {
                     callBackTaskBll.Update(taskEntity);
                     PageHelper.ExecuteJS("Complete('联络信息成功！');");
                     //PageHelper.ShowMessage("联络信息成功！", true);
                 }
                 else
                 {
                     PageHelper.ShowMessage(this.Page, "联络信息失改！");
                 }
             }

        }
    }
}
